package com.icoolkj.learn.juc.base;

import lombok.extern.slf4j.Slf4j;

/**
 * @author icoolkj
 * @version 1.0
 * @description 线程 sleep 方法
 * @createDate 2025/03/04 10:40
 */
@Slf4j
public class ThreadSleepDemo {
    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e); // 中断异常 java.lang.InterruptedException: sleep interrupted
                }
                log.info("{} 线程执行完成", Thread.currentThread().getName());
            }
        }, "t1"); // java 层面线程状态：新建 NEW
        thread1.start();

        log.info("线程 t1 的状态：{}", thread1.getState()); // java 层面线程状态：就绪 RUNNABLE

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        log.info("线程 t1 的状态：{}", thread1.getState()); // java 层面线程状态: TIMED_WAITING

        //thread1.interrupt(); // main 线程调用中断线程1

        // 有 sleep 的无限循环
//        Thread threadWithSleep = new Thread(() -> {
//            while (true) {
//                try {
//                    // 线程暂停 100 毫秒
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                    return;
//                }
//            }
//        });
//        threadWithSleep.start();
//        threadWithSleep.interrupt();



    }
}
